package com.CGFrame.struts.action.jobSchedule;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.CGFrame.struts.bean.make.MakeBean;
import com.CGFrame.struts.bean.unit.UnitBean;

public class ChangeStatusAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String result=request.getParameter("result");
		// result : makeId,statusId;makeId,statusId;makeId,statusId;
		int unitId = 0;
		if(result!=null && result!="")
		{
			HttpSession session = request.getSession();
			String reguserId = (String)session.getAttribute("RegUserID");
			MessageResources ms=getResources(request);
			Locale locale = getLocale(request);
			String[] couple=result.split(";");
			for(int i=0;i<couple.length;i++){
				String[] one=couple[i].split(",");
				MakeBean mb=new MakeBean(Integer.valueOf(one[0]));
				if (unitId ==0) unitId = mb.getUnitId();
				else if (unitId != mb.getUnitId())
				{
					return null;
				}
				String resultStatus =mb.changeStatusById(one[1]); 
				if (resultStatus!=null) // null means  status hasnot changed
				{
					UnitBean unitBean = new UnitBean(mb.getType(),mb.getUnitId());
					String comments = mb.getName()+" --> "+ ms.getMessage(locale,resultStatus);
					unitBean.addComments(reguserId,comments);
					mb.sendRelativeMessage(reguserId,"-->"+ ms.getMessage(locale,resultStatus));
				}
			}
		}
		request.setAttribute("ObjectId",unitId);
		return mapping.findForward("success");
	}
	
}
